package com.his.his_system.controller.VisitController.ZFB;
import com.alipay.api.AlipayApiException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/app/")
public class AliPayController {

    /**
     * 账号 ： jdxhhy1495@sandbox.com
     * 密码 ：111111
     * 支付密码：111111
     */

    public static Integer ii;

    public static Integer registrationid;

    public static  String pcinfo ;

    @RequestMapping(value = "alipay",method = RequestMethod.GET)
    public AlipayResponse alipayPage(Shop shop ,HttpServletRequest request, HttpServletResponse resp){
        System.out.println(shop.toString());
        String url = getClientIpAddress(request);
        System.out.println(url);
        if(shop.getPc().equals("pc")){
            pcinfo="http://localhost:8686/app/payOrRef";
        }else {
            pcinfo="http://"+url+":5173/#/pages/page_guahao?v-28c7b3d5=&type=page&url=%252Fpages%252Fpage_guahao&v28c7b3d5=";
        }
        ii=shop.getI();
        registrationid=shop.getRegistrationId();
        AliPay ap = new AliPay();
        String name = shop.getName();
        String price = shop.getPrice();
        AlipayBean alipayBean = new AlipayBean();
        //商品单号
        alipayBean.setOut_trade_no("18771557979");
        //支付金额
        alipayBean.setTotal_amount(price);

        //商品描述
        alipayBean.setBody("HIS收费");
        //商品名称
        alipayBean.setSubject(name);
        AlipayResponse ar = new AlipayResponse();
        try {
            String alipayPageString = ap.pay(alipayBean);
            ar.setPageHtml(alipayPageString);
        }catch (AlipayApiException e){
            e.printStackTrace();
        }
        return ar;
    }
    @RequestMapping("displayshow")
    public  void  displayshow(HttpServletResponse resp){
        try {
            resp.sendRedirect("http://localhost:8080/index");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    public String getClientIpAddress(HttpServletRequest request) {
        String ipAddress = request.getHeader("X-Forwarded-For");

        if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("Proxy-Client-IP");
        }

        if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }

        if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("HTTP_CLIENT_IP");
        }

        if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("HTTP_X_FORWARDED_FOR");
        }

        if (ipAddress == null || ipAddress.isEmpty() || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getRemoteAddr();
        }

        return ipAddress;
    }
}
